/**
 * Copyright (C) 2006 OQube 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *  
 * author: Arnaud Bailly - abailly AT oqube DOT com
 * created: 13 juin 2006
 */
package speculoos.beans;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @author nono
 *
 */
public class SyntheticMap implements MapStructure,Map {

	/*
	 * a list of classes/maps this map inherits from
	 */
	private List inherits = new ArrayList();

	private Map map = new HashMap();
	
	private String name;
	
	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return Returns the inherits.
	 */
	public List getInherits() {
		return inherits;
	}

	/**
	 * @param inherits The inherits to set.
	 */
	public void setInherits(List inherits) {
		this.inherits = inherits;
	}
	
	/**
	 * Adds another parent to this map's ancestors.
	 * 
	 * @param o
	 */
	public void addInherits(Object o) {
		this.inherits.add(o);
	}

	/**
	 * Return the (flattened) list of all fields from 
	 * parent maps.
	 * This method traverses the class hirarchy of this map and 
	 * collects all fields definition, with the exclusion of the fields
	 * from the superclass.
	 *  
	 * @return a List<MapEntry> object. May be empty or null.
	 */
	public Collection getInheritedFields() {
		List l = new ArrayList();
		Object sc = getSuperclass();
		for(Iterator i= inherits.iterator();i.hasNext();) {
			MapStructure ms = (MapStructure)i.next();
			if(ms == sc ) 
				continue;
			l.addAll(ms.getFields());
			l.addAll(ms.getInheritedFields());
		}
		return l;
	}
	
	/**
	 * Return the superclass of this Map.
	 * The superclass is the first instance of {@see BeanMap} in 
	 * the hierarchy of this structure that is a concrete (not abstract)
	 * class. 
	 *
	 * @return a MapStructure instance or null.
	 */
	public Object getSuperclass() {
		for(Iterator i= inherits.iterator();i.hasNext();) {
			try {
				BeanMap ms = (BeanMap)i.next();
				if(ms.isConcrete())
					return ms;
			}catch(ClassCastException e){
				// NOP
			}
		}
		return null;
	}

	/*
	 *  (non-Javadoc)
	 * @see speculoos.jndi.MapStructure#getFields()
	 */
	public Collection getFields() {
		return map.values();
	}

	
	public String getName() {
		return name;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#get(java.lang.Object)
	 */
	public Object get(Object key) {
		return map.get(key);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
	 */
	public Object put(Object arg0, Object arg1) {
		return map.put(arg0, arg1);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#putAll(java.util.Map)
	 */
	public void putAll(Map arg0) {
		map.putAll(arg0);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#clear()
	 */
	public void clear() {
		map.clear();
	}

	/* (non-Javadoc)
	 * @see java.util.Map#containsKey(java.lang.Object)
	 */
	public boolean containsKey(Object key) {
		return map.containsKey(key);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#containsValue(java.lang.Object)
	 */
	public boolean containsValue(Object value) {
		return map.containsValue(value);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#entrySet()
	 */
	public Set entrySet() {
		return map.entrySet();
	}

	/* (non-Javadoc)
	 * @see java.util.Map#equals(java.lang.Object)
	 */
	public boolean equals(Object o) {
		return map.equals(o);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#hashCode()
	 */
	public int hashCode() {
		return map.hashCode();
	}

	/* (non-Javadoc)
	 * @see java.util.Map#isEmpty()
	 */
	public boolean isEmpty() {
		return map.isEmpty();
	}

	/* (non-Javadoc)
	 * @see java.util.Map#keySet()
	 */
	public Set keySet() {
		return map.keySet();
	}

	/* (non-Javadoc)
	 * @see java.util.Map#remove(java.lang.Object)
	 */
	public Object remove(Object key) {
		return map.remove(key);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#size()
	 */
	public int size() {
		return map.size();
	}

	/* (non-Javadoc)
	 * @see java.util.Map#values()
	 */
	public Collection values() {
		return map.values();
	}
}
